﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Model;


namespace DAL
{
  public  class EarningsDAL
    {
        SqlConnection str = new SqlConnection("Data Source=PC201609040948;Initial Catalog=TianTianDai_Db;Persist Security Info=True;User ID=sa;pwd=123456");
        /// <summary>
        /// 加入
        /// </summary>
        /// <returns></returns>
        public List<Earnings> ShowEarnings()
        {
            str.Open();
            string sql = "select * from Earnings";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<Earnings> listEarnings = new List<Earnings>();
            foreach (DataRow item in ta.Rows)
            {
                Earnings g = new Earnings();
                g.EarningsId = Convert.ToInt32(item["EarningsId"]);
                g.EarningsScope = item["EarningsScope"].ToString();
                listEarnings.Add(g);
            }
            return listEarnings;

        }
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteEarnings(int id)
        {
            str.Open();
            string sql = "delete Earnings where EarningsId='" + id + "'";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int Earnings_Add(Model.Earnings p)
        {
            str.Open();
            string sql = "insert into Earnings values('" + p.EarningsScope + "')";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 显示详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Earnings ShowEarningsX(int id)
        {
            str.Open();
            string sql = "select * from Earnings where EarningsId='" + id + "'";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<Earnings> listEarnings = new List<Earnings>();
            foreach (DataRow item in ta.Rows)
            {
                Earnings g = new Earnings();
                g.EarningsId = Convert.ToInt32(item["EarningsId"]);
                g.EarningsScope = item["EarningsScope"].ToString();
                listEarnings.Add(g);
            }
            return listEarnings.FirstOrDefault();
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int UpdateEarnings(Earnings data)
        {
            str.Open();
            string sql = "update Earnings set  EarningsScope='" + data.EarningsScope + "' where ProjectId='" + data.EarningsId + "'";
            SqlCommand com = new SqlCommand(sql, str);
            int i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public List<Earnings> SeleteEarningst(string name)
        {
            str.Open();
            string sql = "selete EarningsScope,EarningsId from Earnings where EarningsScope like '" + name + "'";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            List<Earnings> listEarnings = new List<Earnings>();
            foreach (DataRow item in ta.Rows)
            {
                Earnings g = new Earnings();
                g.EarningsId = Convert.ToInt32(item["EarningsId"]);
                g.EarningsScope = item["EarningsScope"].ToString();
                listEarnings.Add(g);
            }
            return listEarnings;
        }
    }
}
